package com.myzl.web.controller.test;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("/cmd")
@Slf4j
@Data
@Api("命令测试")
public class CmdController {

    @ApiOperation("pythonTest")
    @GetMapping("/pythonTest")
    public void pythonTest() {
        try {
            log.info("---------------------开始执行命令----------------------");
            // 执行py文件
            Process proc = Runtime.getRuntime().exec("python3 /home/www/wwwroot/ai/PaddleClas/deploy/python/predict_cls.py -c /home/www/wwwroot/ai/model/inference.yaml -o Global.infer_imgs='/home/www/wwwroot/ai/test/'");
            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            String line = null;
            while ((line = in.readLine()) != null) {
                log.info(line);
            }
            in.close();
            proc.waitFor();
            log.info("---------------------结束执行命令----------------------");
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
            log.error("---------------------执行命令异常----------------------");
            log.error("执行命令异常",e);
        }
    }
}
